package com.kakavr.work.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.beust.jcommander.internal.Maps;
import com.google.common.base.Objects;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.util.SessionUtils;
import com.kakavr.work.service.IWorkRecordService;
/**
 * 店铺人员上班打卡
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("workRecord")
public class WorkRecordController extends BaseController {
	@Resource
	private IWorkRecordService workRecordService;
	
	/**
	 * 待上班人员列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectWorkUserList", method = RequestMethod.GET)
	public Map<String, Object> selectWorkUserList(HttpServletRequest request){
		Map<String, Object> map = Maps.newHashMap();
		map.put("storeId", SessionUtils.getUserId());
		return MsgResult.okResultData(workRecordService.selectWorkUserList(map));
	}
	
	/**
	 * 上班打卡
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Map<String, Object> addWork(HttpServletRequest request){
		String empId = request.getParameter("empIds");
		if(StringUtils.isBlank(empId)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "上班人员");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("storeId", SessionUtils.getUserId());
		map.put("time", DateTime.now().toDate());
		if(Objects.equal(1, SessionUtils.getCurUser().get("isResident"))){
			map.put("staffType", 1);
			map.put("userId", "admin_id");
		}else{
			map.put("staffType", 0);
			map.put("userId", "emp_id");
		}
		map.put("userList", empId.split(","));
		workRecordService.addWorkRecord(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 下班打卡
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public Map<String, Object> updateWork(HttpServletRequest request){
		Map<String, Object> map = Maps.newHashMap();
		map.put("storeId", SessionUtils.getUserId());
		map.put("time", DateTime.now().toDate());
		workRecordService.updateWorkRecord(map);
		return MsgResult.okResult();
	}

}
